package org.example.course.service.impl;

import org.example.course.common.DataGridView;
import org.example.course.domain.Check;
import org.example.course.domain.Checkevaluation;
import org.example.course.mapper.CheckMapper;
import org.example.course.mapper.CheckevaluationMapper;
import org.example.course.service.CheckevaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname CheckevaluationServiceImpl
 * @Description TODO
 * @Date 2020/5/9 15:23
 * @Created by
 */
@Service
public class CheckevaluationServiceImpl implements CheckevaluationService {
    @Autowired
    private CheckevaluationMapper mapper;


    @Autowired
    private CheckMapper checkMapper;

    @Override
    public DataGridView findBycourseId(String id) {
        List<Checkevaluation> list = mapper.findAllByCourseId(Integer.valueOf(id));
        //查询合计行数据
        List<Check> checks = checkMapper.findAllByCourseId(Integer.valueOf(id));
        Checkevaluation checkevaluation = new Checkevaluation();

        for (int i = 0; i < checks.size(); i++) {
            //合计数
            Integer ratio = checks.get(i).getRatio();
            checkevaluation.setResults("100");
            setHeji(checkevaluation, i, ratio);
        }
        return new DataGridView(list,checkevaluation);
    }

    private void setHeji(Checkevaluation checkevaluation, int i, Integer ratio) {
        switch (i + 1) {
            case 1:
                checkevaluation.setCheck1(String.valueOf(ratio));
                break;
            case 2:
                checkevaluation.setCheck2(String.valueOf(ratio));
                break;
            case 3:
                checkevaluation.setCheck3(String.valueOf(ratio));
                break;
            case 4:
                checkevaluation.setCheck4(String.valueOf(ratio));
                break;
            case 5:
                checkevaluation.setCheck5(String.valueOf(ratio));
                break;
            default:
                break;
        }
    }

    @Override
    public boolean create(Checkevaluation checkevaluation) {
        int msg = mapper.insert(checkevaluation);
        return msg > 0;
    }

    @Override
    public boolean update(Checkevaluation checkevaluation) {
        int msg = mapper.updateByPrimaryKeySelective(checkevaluation);
        return msg > 0;
    }
}
